package com.hfut.manage.controller;


import com.hfut.manage.pojo.Result;
import com.hfut.manage.pojo.Title;
import com.hfut.manage.service.ITitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/title")
public class TitleController {

    @Autowired
    private ITitleService titleService;

    @GetMapping
    public Result List() {
        List<Title> titleList = titleService.lambdaQuery().orderByAsc(Title::getId).list();
        return Result.success(titleList);
    }

    @GetMapping("/{name}")
    public Result titleList(@PathVariable("") String name) {
        List<Title> titleList = titleService.lambdaQuery().eq(Title::getName,name).orderByAsc(Title::getId).list();
        return Result.success(titleList);
    }

    @PostMapping
    public Result save(@RequestBody Title title) {
        boolean success = titleService.save(title);
        if (!success) {
            return Result.error(500, "插入失败");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id) {
        titleService.removeById(id);
        return Result.success();
    }
}
